package ru.vasilij.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class FilmPage extends InternalPage {
	
	public FilmPage(PageManager pages) {
		super(pages);
	}
	
	@FindBy(css = "nav a[href *= '&own=']")
	private WebElement filmOwnLink;

	@FindBy(css = "nav a[href *= '&seen=']")
	private WebElement userManagementLink;

	@FindBy(css = "nav a[href *= '?go=edit']")
	private WebElement folmEditLink;
	
	@FindBy(css = "nav a[href = 'javascript:void(0);']")
	private WebElement filmDeleteLink;

	public String getId() {
		String url = driver.getCurrentUrl();
		String[] params = url.split("&");
		return params[1].split("=")[1];
	}
	
	public String getFilmNameAndYear() {
		return driver.findElement(By.xpath("//h2")).getText().trim();
	}
	
	public InternalPage clickFilmDelete() {
		filmDeleteLink.click();
		wait.until(alertIsPresent()).accept();
		return pages.internalPage;
    }
	
	public FilmPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.cssSelector("div#movie div.maininfo_full")));
		return this;
	}
		
}
